package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.wemedia.pojos.WmNews;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
public class ArticleListener {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = "news.down.up.topic")
    public void getMessage(String message) {
        if (StringUtils.isBlank(message)) {
            return;
        }
        WmNews wmNews = JSON.parseObject(message, WmNews.class);
        if (wmNews == null) {
            return;
        }
        Long articleId = wmNews.getArticleId();
        ApArticleConfig articleConfig = apArticleConfigMapper.selectById(articleId);
        if (articleConfig == null) {
            return;
        }
        Short enable = wmNews.getEnable();
        if (enable == 0) {
            articleConfig.setIsDown(true);
        } else {
            articleConfig.setIsDown(false);
        }
        int updateRsult = apArticleConfigMapper.updateById(articleConfig);
        if (updateRsult < 1) {
            return;
        }
    }
}
